articles

Home / DeveloperSection / Articles / Cell formatting of DataGridView in .NET

Cell formatting of DataGridView in .NET

Cell formatting of DataGridView in .NET

Sachindra Singh 19620 11-Feb-2011

What is DataGridView?

Where The DataGridView control is used for displaying the data from the SQL database. Data will display in a table from the database on the DataGridView, using DataAdapter and data logic.

The DataGridView control is a Windows Forms control that gives you the ability to customize and edit tabular data.

It gives you a number of properties, methods, and events to customize its appearance and behavior.

In this demonstration, we will see how we can change BackColor, ForeColor, and Font of a particular cell as well as all cells of a particular row.

Code
public partial class Detail :  Form
    {
        publicDetail()
        {
            InitializeComponent();
        }
        SqlConnection con;//creating SqlConnection class variable con
        FontDialog fnt=new FontDialog();//creating object(fnt) of FontDialog
        ColorDialog cd= new ColorDialog();//creating object(cd) of ColorDialog
        intri, ci,ro,flag=0,f=0,first=1;//creating int variable
        privatevoidDetail_Load(objectsender, EventArgse)
        {
            try
            {
                con =new SqlConnection("data source=uttam-pc\\mindstick; user id=sa; password=mindstick; initial catalog= detail");//creating connection from database
                con.Open();//connection opening
                SqlCommandcmd= new SqlCommand("select * from StudentDetail", con);//executing SQL query
                SqlDataReaderdr= cmd.ExecuteReader();//SqlDataReader class variable storing data
                DataTabledt= newDataTable();//DataTable, which represents one table of in-memory relational data, can be created and used independently
                dt.Load(dr);//Load method is use for load all data from dr in dt
 
                DataGridViewButtonColumnbcol= new DataGridViewButtonColumn();//creating object of DataGridViewButtonColumn for Color
                bcol.HeaderText="Change Color ";//column name providing of button(bcol)
                bcol.Text="Color";//this line defining text of button
                bcol.Name="Color1";//this line defining name of button
                bcol.UseColumnTextForButtonValue=true;
                dataGridView1.Columns.Add(bcol);//adding color button in datagridview
 
                DataGridViewButtonColumnbcol1= new DataGridViewButtonColumn();//creating object of DataGridViewButtonColumn For
                bcol1.HeaderText="Change Font ";//column name providing of button(bcol)
                bcol1.Text="Font";//this line defining text of button
                bcol1.Name="Font";//this line defining name of button
                bcol1.UseColumnTextForButtonValue=true;
                dataGridView1.Columns.Add(bcol1);//adding font button in datagridview
 
                dataGridView1.DataSource=dt;//loading data in datagridview
            }
            catch (Exceptionex)//this line will invoke when error prone situation will raise
            {
                MessageBox.Show(ex.Message);//this line will show error message
            }
            finally
            {
                con.Close();//connection closing
            }
        }
        privatevoid dataGridView1_CellMouseClick(objectsender, DataGridViewCellMouseEventArgse)
        {
            if (e.ColumnIndex==0)//checking condition
            {
                cd.ShowDialog();//this line will show color dialog
                if (flag==0)//checking condition if flag store 0
                {
                    dataGridView1.Rows[e.RowIndex].Cells[2].Style.BackColor=cd.Color;//this line will change color of cell[2]
                    dataGridView1.Rows[e.RowIndex].Cells[3].Style.BackColor=cd.Color;//this line will change color of cell[3]
                    dataGridView1.Rows[e.RowIndex].Cells[4].Style.BackColor=cd.Color;//this line will change color of cell[4]
                    dataGridView1.Rows[e.RowIndex].Cells[5].Style.BackColor=cd.Color;//this line will change color of cell[5]
                }
                if (flag==1 &&ro==ri)//checking condition
                {
                    if (f==1)//checking condition
                    {
                        dataGridView1.Rows[ri].Cells[ci].Style.BackColor=cd.Color;//this line will change backcolor of particular cell ri store index number of cell and ci store cell index  of particular cell
                        f=0;//initializing f from 0
                    }
                    else
                        dataGridView1.Rows[ri].Cells[ci].Style.ForeColor=cd.Color;//this line will change forecolor of particular cell ri store index number of cell and ci store cell index of particular cell
                }
            }
            if (e.ColumnIndex==1)//checking condition
            {
                fnt.ShowDialog();//this line will show font dialog
                if (flag==0)//checking condition if flag store 0
                {
                    dataGridView1.Rows[e.RowIndex].Cells[2].Style.Font=fnt.Font;//this line will change font of cell[2]
                    dataGridView1.Rows[e.RowIndex].Cells[3].Style.Font=fnt.Font;//this line will change font of cell[3]
                    dataGridView1.Rows[e.RowIndex].Cells[4].Style.Font=fnt.Font;//this line will change font of cell[4]
                    dataGridView1.Rows[e.RowIndex].Cells[5].Style.Font=fnt.Font;//this line will change font of cell[5]
                }
                if (flag==1 &&ro==ri)//checking condition
                {
                    dataGridView1.Rows[ri].Cells[ci].Style.Font=fnt.Font;//this line will change backcolor of particular cell ri store index number of cell and ci store cell index  of particular cell
                }
            }
        }
        privatevoid dataGridView1_CellDoubleClick(objectsender, DataGridViewCellEventArgse)
        {
            if (first==1)//checking condition
            {
                ro =dataGridView1.Rows[e.RowIndex].Index;//variable ro storing row index of cell
                first--;//decrement by 1
            }
            f=1;//f initializing from 1
            flag=1;//flag initializing from 1
            ri=dataGridView1.Rows[e.RowIndex].Index;//variable ri storing row index of cell
            ci=dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ColumnIndex;//variable ci storing column index of cell     
        }
        privatevoidbtnClose_Click(objectsender, EventArgse)
        {
            Application.Exit();//apllication will be close
        }
        privatevoid dataGridView1_MouseDoubleClick(objectsender, MouseEventArgse)
        {
           
        }
        privatevoid dataGridView1_CellContentClick_1(objectsender, DataGridViewCellEventArgse)
        {
            ri=dataGridView1.Rows[e.RowIndex].Index;//variable ri storing row index of cell
            ci=dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ColumnIndex;//variable ci storing column index of cell
            if (first==1)//checking condition
            {
                ro =dataGridView1.Rows[e.RowIndex].Index;//variable ro storing row index of cell
                first--;//decrement by 1
            }
            if (ci==0||ci==1)//checking condition
           {
                flag=0;//flag initializing from 1
                ro =dataGridView1.Rows[e.RowIndex].Index;//variable ro storing row index of cell
            }
            else
                flag=1;//flag initializing from 1
        }
        privatevoid dataGridView1_CellContentDoubleClick(objectsender, DataGridViewCellEventArgse)
        {
            stringvalue=dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();//storing data of cell in string variable(value) when cell is doubleclick
            UpdateCellupc= new UpdateCell(value);//creating object of form(UpdateCell) and with parameter
            upc.ShowDialog();//this line will show UpdateCell form
            dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value=upc.UpdateValue;//if user done any changes in data by using Updatecell form this line will append that value in particular cell
        }
 
    }
Screen Shot of DataGridView

Cell formatting of DataGridView in .NET

Most of the times we want to update any particular cell of data, in this Demonstration just double click on the cell where to want to update data you will see a new window will be open,

in this Demonstration, I clicked on  that cell where the city is  Varanasi but I want to update city Varanasi from Jabalpur as shown below:

Cell formatting of DataGridView in .NET

After click on the save button city name has changed where city name was Varanasi with Jabalpur as shown below:

Cell formatting of DataGridView in .NET



Updated 22-Sep-2020

Leave Comment

Comments

Liked By